package org.brisling.task.service;

import java.util.Calendar;
import java.util.Date;

import org.brisling.common.SysConfig;
import org.brisling.medical.queryService.QueryAntidoseService;
import org.brisling.medical.queryService.QueryAntimedicalService;
import org.brisling.medical.queryService.QueryBasePatientInfoService;
import org.brisling.medical.queryService.QueryBasemedicalService;
import org.brisling.satisfaction.queryService.QuerySatisBaseinfoService;
import org.brisling.sysman.service.ShortMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledService {

	private static Logger logger = LoggerFactory.getLogger(ScheduledService.class);
	// @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次  
	
	@Autowired
    private QueryAntidoseService qryAntidoseService;
	@Autowired
	private QueryBasemedicalService qryBasemedicalService;
	@Autowired
	private QueryAntimedicalService qryAntimedicalService;
	
	@Autowired
	private QueryBasePatientInfoService qryBasePatientInfoService;	
	
	@Autowired
	private QuerySatisBaseinfoService qrySatisBaseinfoService;
	
	@Autowired
	private ShortMessageService smService;
	
	
//	private int cout = 0;
	/**
	 * 每日执行(定时发送短信)
	 */
	 @Scheduled(cron="0 0/5 8-18 * * MON-FRI")
     public void sendMessage(){
		 
//		 logger.debug("short message send fired : " + cout++);
		 smService.timingSend();
	 }
	 
	/**
	 * 每日执行(启用日志)
	 */
	 @Scheduled(cron="0 0 7 ? * *")
     public void enableLog(){
		 SysConfig.setEnableLog(true);
	 }
	/**
	 * 每日执行(抗菌药物使用强度)
	 */
	 @Scheduled(cron="0 0 2 ? * *")
     public void antiDoseCount(){
		 String threadName =Thread.currentThread().getName();
           System.out.println("进入测试: 线程 - " +threadName); 
           Date time_start = Calendar.getInstance().getTime();
           
           
           logger.debug("ANTI start at:" + time_start);
           
           qryAntidoseService.autoMonitor(null, null);           
           
           Date time_stop = Calendar.getInstance().getTime();
   		   Long seconds = time_stop.getTime() - time_start.getTime();
   		   
   		   logger.debug("ANTI Dose stop at:" + time_stop);
   		   
   		   logger.debug("ANTI Dose time consume:" + seconds);
          
     }  
	 
	 /**
	  * 每日执行(抗菌药物使用率)
	  */
	 @Scheduled(cron="0 0 2 ? * *")
     public void antiMedicalCount(){
		 String threadName =Thread.currentThread().getName();
		 System.out.println("进入测试: 线程 - " +threadName); 
		 Date time_start = Calendar.getInstance().getTime();
		 logger.debug("ANTI MEDICAL start at:" +Calendar.getInstance().getTime());
		 qryAntimedicalService.autoMonitor(null, null);
         logger.debug("ANTI MEDICAL END at :" + Calendar.getInstance().getTime());
         Date time_stop = Calendar.getInstance().getTime();
         Long seconds = time_stop.getTime() - time_start.getTime();
         
         logger.debug("ANTI MEDICAL time consume:" + seconds);
	 }
	 
	 /**
	  * 每日执行(基本药物使用)
	  */
	 @Scheduled(cron="0 0 2 ? * *")
     public void baseMedicalCount(){
		 String threadName =Thread.currentThread().getName();
		 System.out.println("进入测试: 线程 - " +threadName); 
		 Date time_start = Calendar.getInstance().getTime();
		 logger.debug("BASE MEDICAL start at:" +Calendar.getInstance().getTime());
         qryBasemedicalService.autoMonitor(null, null);
         logger.debug("BASE MEDICAL END at :" + Calendar.getInstance().getTime());
         Date time_stop = Calendar.getInstance().getTime();
         Long seconds = time_stop.getTime() - time_start.getTime();
         
         logger.debug("BASE MEDICAL time consume:" + seconds);
	 }
	 
	 /**
	  * 每日执行(患者基本情况)
	  */
	 @Scheduled(cron="0 0 2 ? * *")
     public void basePatientInfoCount(){
		 String threadName =Thread.currentThread().getName();
		 System.out.println("进入测试: 线程 - " +threadName); 
		 Date time_start = Calendar.getInstance().getTime();
		 logger.debug("BASE PatientInfo start at:" +Calendar.getInstance().getTime());
		 qryBasePatientInfoService.autoTask(null, null);
         logger.debug("BASE PatientInfo END at :" + Calendar.getInstance().getTime());
         Date time_stop = Calendar.getInstance().getTime();
         Long seconds = time_stop.getTime() - time_start.getTime();
         
         logger.debug("BASE PatientInfo time consume:" + seconds);
	 }
	 
	 /**
	  * 满意度统计类
	  */
	 @Scheduled(cron="0 0 2 ? * *")
     public void baseSatisfactionInfoCount(){
		 String threadName =Thread.currentThread().getName();
		 System.out.println("进入测试: 线程 - " +threadName); 
		 Date time_start = Calendar.getInstance().getTime();
		 logger.debug("BASE PatientInfo start at:" +Calendar.getInstance().getTime());
		 qrySatisBaseinfoService.autoTask(null, null);
         logger.debug("BASE PatientInfo END at :" + Calendar.getInstance().getTime());
         Date time_stop = Calendar.getInstance().getTime();
         Long seconds = time_stop.getTime() - time_start.getTime();
         
         logger.debug("BASE MEDICAL time consume:" + seconds);
	 }
	 
	 
	 /**
	  * 统计指标更新
	  */
	 @Scheduled(cron="0 30 2 ? * *")
     public void updateIndicate(){
		 String threadName =Thread.currentThread().getName();
		 System.out.println("进入测试: 线程 - " +threadName); 
		 
		 qryAntidoseService.autoUpdateStandard(null, null);
         
		 qryAntimedicalService.autoUpdateStandard(null, null);
		 
		 qryBasemedicalService.autoUpdateStandard(null, null);
	 }
	 
	 
}
